package GUILayer;

import java.awt.*;
import java.util.List;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import GUIadapter.EmployeeModel;
import GUIadapter.PanelTableModelAdapter;
import CtrlLayer.*;
import ModelLayer.Employee;
import javax.swing.LayoutStyle.ComponentPlacement;

@SuppressWarnings("serial")
public class PanelMedarbejder extends JPanel implements Updateable
{
	private PersonCtrl PC;
	private List<Employee> employeeList;
	private PanelTableModelAdapter panelListe;
	private int indexEmployee;
			
	private PopupOpretMedarbejder popupOpretMedarbejder;
	private PopupRedigerMedarbejder popupRedigerMedarbejder;

	public PanelMedarbejder()
	{
		setPreferredSize(new Dimension(584, 392));
		setMinimumSize(new Dimension(0, 0));
		PC = new PersonCtrl();
		popupOpretMedarbejder = new PopupOpretMedarbejder(this);
		popupRedigerMedarbejder = new PopupRedigerMedarbejder(this);

		JButton btnRedigerMedarbejder = new JButton("Rediger Medarbejder");
		btnRedigerMedarbejder.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				popupRedigerMedarbejder.resetMedarbejder();
				popupRedigerMedarbejder.setVisible(true);
			}
		});

		JButton btnOpretMedarbejder = new JButton("Opret Medarbejder");
		btnOpretMedarbejder.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				popupOpretMedarbejder.clearFields();
				popupOpretMedarbejder.setVisible(true);
			}
		});
		
		employeeList = PC.getEmployeeList();
		panelListe = new PanelTableModelAdapter(new EmployeeModel(employeeList));
		panelListe.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Medarbejder Kartotek", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelListe.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelListe.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
			{
				@Override
				public void valueChanged(ListSelectionEvent event)
				{
					if (event.getValueIsAdjusting() == false)
					{
						indexEmployee = panelListe.getTable().getSelectedRow();
					}
				}
			});

				
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelListe, GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnRedigerMedarbejder)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnOpretMedarbejder)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelListe, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOpretMedarbejder)
						.addComponent(btnRedigerMedarbejder))
					.addContainerGap())
		);
		setLayout(groupLayout);
	}
		
	@Override
	public void update(){
		panelListe.getTable().revalidate();
		panelListe.getTable().repaint();
	}
}
